package com.zhz.system.controller;

import com.zhz.common.controller.BaseController;
import com.zhz.common.tool.mybatisplus.base.core.contract.Page;
import com.zhz.common.tool.mybatisplus.base.core.contract.R;
import com.zhz.model.system.contract.param.RoleQueryParam;
import com.zhz.system.biz.RoleBizService;
import com.zhz.system.contract.request.*;
import com.zhz.system.contract.response.RoleFindAllResponse;
import com.zhz.system.contract.response.RoleFindByIdResponse;
import com.zhz.system.contract.response.RoleFindByPageResponse;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhouhengzhe
 * @date 2023/4/2
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleBizService roleBizService;

    @ApiModelProperty("分配权限")
    @PostMapping("/assignPrem")
    public R<Void> assignPrem(@RequestBody RoleAssignPermRequest roleAssignPermRequest) {
        //1.调用service完成权限分配
        roleBizService.assignPerms(roleAssignPermRequest.getId(), roleAssignPermRequest.getPermIds());
        return R.ok();
    }

    @ApiOperation("添加角色")
    @PostMapping("/add")
    public R<Void> add(@RequestBody RoleSaveRequest roleSaveRequest) {
        roleSaveRequest.setCompanyId(companyId);
        roleBizService.save(roleSaveRequest);
        return R.ok();
    }

    @ApiOperation("更新角色")
    @PostMapping("/update")
    public R<Void> update(@RequestBody @Validated RoleUpdateRequest roleUpdateRequest) {
        roleBizService.update(roleUpdateRequest);
        return R.ok();
    }

    @ApiOperation("删除角色")
    @PostMapping("delete")
    public R<Void> delete(@RequestBody @Validated RoleDeleteRequest roleDeleteRequest) {
        roleBizService.delete(roleDeleteRequest);
        return R.ok();
    }

    @ApiOperation("根据ID获取角色信息")
    @PostMapping("/findById")
    public R<RoleFindByIdResponse> findById(@RequestBody RoleFindByIdRequest roleFindByIdRequest) {
        return R.ok(roleBizService.findById(roleFindByIdRequest));
    }

    @ApiOperation("分页查询角色")
    @PostMapping("/findByPage")
    public R<Page<RoleFindByPageResponse>> findByPage(@RequestBody RoleQueryParam roleQueryParam) throws Exception {
        roleQueryParam.setCompanyId(companyId);
        return R.ok(roleBizService.findByPage(roleQueryParam));
    }

    @ApiOperation("查询所有角色")
    @PostMapping("findAll")
    public R<RoleFindAllResponse> findAll() {
        return R.ok(roleBizService.findAll(companyId));
    }

}
